#!/usr/env/bin python
# -*- coding: utf-8 -*-

# @Time    : 2020-11-25 9:40 上午
# @Author  : yangdy
# @File    : 2020-11-25.1370.上升下降字符串
# @Software: PyCharmCE
from tools.my_methods import *


class Solution:
    def sortString(self, s: str) -> str:
        c, total = [0 for i in range(26)], 0
        for si in s:
            c[ord(si) - ord('a')] += 1
            total += 1
        ans = ''
        while total:
            for i in range(26):
                if c[i] > 0 and total > 0:
                    ans += chr(ord('a') + i)
                    c[i] -= 1
                    total -= 1
                elif total <= 0:
                    break
            for i in range(25, -1, -1):
                if c[i] > 0 and total > 0:
                    ans += chr(ord('a') + i)
                    c[i] -= 1
                    total -= 1
                elif total <= 0:
                    break
        return ans


questions = [
    ('aaaabbbbcccc',),
    ('rat',),
    ('leetcode',),
    ('ggggggg',),
    ('spo',),
]

work(questions, lambda q: Solution().sortString(q[0]))
